package com.api.shopapi.mapper;

import com.api.shopapi.entity.DeviceQzXuetangyi;
import com.api.shopapi.entity.dto.DeviceQzXuetangyiDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface DeviceQzXuetangyiMapper {

    @Insert("insert into device_qzxuetangyi (DeviceQzXuetangyiCode,UserCode,SN,BindTime) values " +
            "(#{deviceQzXuetangyi.DeviceQzXuetangyiCode},#{deviceQzXuetangyi.UserCode},#{deviceQzXuetangyi.SN},#{deviceQzXuetangyi.BindTime})")
    int insert(@Param("deviceQzXuetangyi") DeviceQzXuetangyi deviceQzXuetangyi);

    @Insert("update  device_qzxuetangyi set UserCode=#{deviceQzXuetangyi.UserCode},SN=#{deviceQzXuetangyi.SN},BindTime=#{deviceQzXuetangyi.BindTime} " +
            "where DeviceQzXuetangyiCode=#{deviceQzXuetangyi.DeviceQzXuetangyiCode}")
    int update(@Param("deviceQzXuetangyi") DeviceQzXuetangyi deviceQzXuetangyi);

    @Select("select * from device_qzxuetangyi where UserCode=#{userCode} limit 1")
    DeviceQzXuetangyi getByUserCode(@Param("userCode") String userCode);

    @Select("select * from device_qzxuetangyi where SN=#{sn} limit 1")
    DeviceQzXuetangyi getBySN(@Param("sn") String sn);


    @Select("<script>" +
            "select dq.*,u.Name as UserName,u.Phone  from device_qzxuetangyi dq left join user u on dq.UserCode = u.UserCode " +
            "left join user_device ud on ud.UserCode = dq.UserCode " +
            "where dq.SN is not null " +
            "and ud.QZXueTangYi = 1 " +
            "<if test ='phone !=null and phone!=\"\" '>" +
            " and u.Phone like concat('%',#{phone},'%') " +
            "</if>" +
            "<if test ='sn !=null and sn!=\"\" '>" +
            " and dq.SN =#{sn} " +
            "</if>" +
            "</script>")
    List<DeviceQzXuetangyiDTO> getPage(@Param("phone") String phone, @Param("sn") String sn);

}
